---
category: Array
contributors:
    - TangSY
created: '2020-05-13'
title: Partition an array based on a condition
updated: '2021-10-22'
---

**JavaScript version**

```js partition.js
const partition = (arr, criteria) => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);
```

**TypeScript version**

```ts partition.ts
const partition = <T,_>(arr: T[], criteria: (a: T) => boolean): T[][] => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);
```

**Example**

```js example.js
partition([1, 2, 3, 4, 5], (n) => n % 2); // [[1, 3, 5], [2, 4]]
```
